package com.ctg.auth.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 登陆拦截器
 */
public class LoginInterceptor extends HandlerInterceptorAdapter{
	public static final String  NO_MATCH_PATH1=".*/(login).*";
	public static final String NO_MATCH_PATH2=".*/(gologin).*";
	public static String SESSION_TOKEN;
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken = (String) request.getSession().getAttribute("SESSION_TOKEN");

        // 初始化拦截器，设置不拦截路径
        String noMatchPath1=NO_MATCH_PATH1;
        String noMatchPath2=NO_MATCH_PATH2;
        String path=request.getServletPath();

        System.out.println("资源请求路径："+path);
        if(path.matches(noMatchPath1)||path.matches(noMatchPath2)){
            // 授权路径，不拦截
            return  true;
        } else if(null == userToken || "".equals(userToken)) {
            // 找不到用户Token，重定位到登录
            response.sendRedirect(request.getContextPath() + "/login");
            return  false;
        } else {
            // 设置扩展
            return  true;
        }
    }
}
